﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WorldBossMgr : MonoBehaviour {

    private Dictionary<int, SpriteRenderer> _tileSprites = new Dictionary<int, SpriteRenderer>(); //当前显示的地块
    public int m_BossHudId = 0;
    public int m_MonsterId = 0;
    public int m_Score = 0;
    public Vector2 m_mapPos;
    void Start () {
    }  
      
      
    void Update () {
        
    }

    public void CreateGrid()
    {
        for (int i = 0; i < 4; i++)
        {
            SpriteRenderer sr = ModuleMgr.Instance.BigMapRes.GetWorldBossGo().AddChild<SpriteRenderer>();
            ModuleMgr.Instance.BigMapRes.SetSpriteRenderer(sr, "belong_1");
            if (i == 0)
            {
                sr.gameObject.transform.localPosition = Vector3.zero;
            }
            else if (i == 1)
            {
                sr.gameObject.transform.localPosition = new Vector3(1, -0.5f, 0);
            }
            else if (i == 2)
            {
                sr.gameObject.transform.localPosition = new Vector3(-1, -0.5f, 0);
            }
            else if (i == 3)
            {
                sr.gameObject.transform.localPosition = new Vector3(0, -1, 0);
            }
            _tileSprites[i] = sr;
        }
    }

    public void ChangeState(int index,bool isMatch)
    {
        if (_tileSprites.ContainsKey(index))
        {
            string pathStr = "";
            if (isMatch)
                pathStr = "belong_1";
            else
                pathStr = "belong_3";
            ModuleMgr.Instance.BigMapRes.SetSpriteRenderer(_tileSprites[index], pathStr);
        }
    }

    public void CheckGridMatch(Vector2 mapPos)
    {
        int cellType1 = ModuleMgr.Instance.BigMap.GetMapType(mapPos);
        ChangeState(0, (cellType1 == MapDefine.Map_Type_Grass || cellType1 == MapDefine.Map_Type_Lava || cellType1 == MapDefine.Map_Type_Sonw || cellType1 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos) <= 0);
        int cellType2 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(1, 0));
        ChangeState(1, (cellType2 == MapDefine.Map_Type_Grass || cellType2 == MapDefine.Map_Type_Lava || cellType2 == MapDefine.Map_Type_Sonw || cellType2 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(1, 0)) <= 0);
        int cellType3 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(0, 1));
        ChangeState(2, (cellType3 == MapDefine.Map_Type_Grass || cellType3 == MapDefine.Map_Type_Lava || cellType3 == MapDefine.Map_Type_Sonw || cellType3 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(0, 1)) <= 0);
        int cellType4 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(1, 1));
        ChangeState(3, (cellType4 == MapDefine.Map_Type_Grass || cellType4 == MapDefine.Map_Type_Lava || cellType4 == MapDefine.Map_Type_Sonw || cellType4 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(1, 1)) <= 0);
    }


    //检测当前屏的空地
    public Vector2 GetFreeGrid()
    {
        Vector2 mapCenterPos = ModuleMgr.Instance.BigMap.MapCenterPos;
        float startX = mapCenterPos.x - 2 * MapDefine.ViewWidth - (MapDefine.ViewHeight - MapDefine.ViewWidth);
        float startY = mapCenterPos.y - (MapDefine.ViewHeight - MapDefine.ViewWidth);
        const int wMax = 2 * (MapDefine.ViewWidth);
        const int hMax = 2 * (MapDefine.ViewHeight);
//        for (int w = 0; w <= wMax; w++)
//        {
//            for (int h = 0; h <= hMax; h++)
//            {
//                for (int c = 0; c <= 1; c++)
//                {
//                    Vector2 mapPos = new Vector2(w, h);
//                    int tileId = MapUtil.GetTileId(mapPos);
//
//                    bool cellMatch1 = false;
//                    bool cellMatch2 = false;
//                    bool cellMatch3 = false;
//                    bool cellMatch4 = false;
//                    int cellType1 = ModuleMgr.Instance.BigMap.GetMapType(mapPos);
//                    cellMatch1 = (cellType1 == MapDefine.Map_Type_Grass || cellType1 == MapDefine.Map_Type_Lava || cellType1 == MapDefine.Map_Type_Sonw || cellType1 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos) <= 0;
//                    int cellType2 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(1, 0));
//                    cellMatch2 = (cellType2 == MapDefine.Map_Type_Grass || cellType2 == MapDefine.Map_Type_Lava || cellType2 == MapDefine.Map_Type_Sonw || cellType2 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(1, 0)) <= 0;
//                    int cellType3 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(0, 1));
//                    cellMatch3 = (cellType3 == MapDefine.Map_Type_Grass || cellType3 == MapDefine.Map_Type_Lava || cellType3 == MapDefine.Map_Type_Sonw || cellType3 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(0, 1)) <= 0;
//                    int cellType4 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(1, 1));
//                    cellMatch4 = (cellType4 == MapDefine.Map_Type_Grass || cellType4 == MapDefine.Map_Type_Lava || cellType4 == MapDefine.Map_Type_Sonw || cellType4 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(1, 1)) <= 0;
//                    Debug.Log(mapPos);
//                    Debug.Log(cellMatch1);
//                    Debug.Log(cellMatch2);
//                    Debug.Log(cellMatch3);
//                    Debug.Log(cellMatch4);
//                    if (cellMatch1 && cellMatch2 && cellMatch3 && cellMatch4)
//                    {
//                        return mapPos;
//                    }
//                }
//            }
//        }

        int size = 4;
        for (int tempX = -size; tempX <= size; tempX++)
        {
            for (int tempY = -size; tempY <= size; tempY++)
            {
                Vector2 mapPos = mapCenterPos + new Vector2(tempX, tempY);

                bool cellMatch1 = false;
                bool cellMatch2 = false;
                bool cellMatch3 = false;
                bool cellMatch4 = false;
                int cellType1 = ModuleMgr.Instance.BigMap.GetMapType(mapPos);
                cellMatch1 = (cellType1 == MapDefine.Map_Type_Grass || cellType1 == MapDefine.Map_Type_Lava || cellType1 == MapDefine.Map_Type_Sonw || cellType1 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos) <= 0;
                int cellType2 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(1, 0));
                cellMatch2 = (cellType2 == MapDefine.Map_Type_Grass || cellType2 == MapDefine.Map_Type_Lava || cellType2 == MapDefine.Map_Type_Sonw || cellType2 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(1, 0)) <= 0;
                int cellType3 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(0, 1));
                cellMatch3 = (cellType3 == MapDefine.Map_Type_Grass || cellType3 == MapDefine.Map_Type_Lava || cellType3 == MapDefine.Map_Type_Sonw || cellType3 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(0, 1)) <= 0;
                int cellType4 = ModuleMgr.Instance.BigMap.GetMapType(mapPos + new Vector2(1, 1));
                cellMatch4 = (cellType4 == MapDefine.Map_Type_Grass || cellType4 == MapDefine.Map_Type_Lava || cellType4 == MapDefine.Map_Type_Sonw || cellType4 == MapDefine.Map_Type_Tree) && CacheMgr.Instance.BigMap.CheckIsCity(mapPos + new Vector2(1, 1)) <= 0;
                
                if (cellMatch1 && cellMatch2 && cellMatch3 && cellMatch4)
                {
                    return mapPos;
                }
            }
        }

        return ModuleMgr.Instance.BigMap.MapCenterPos;
    }
}  